package csu.web.mypetstore.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import csu.web.mypetstore.pojo.DB_Cart;
import csu.web.mypetstore.service.CartService;
import csu.web.mypetstore.mapper.CartMapper;
import csu.web.mypetstore.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, DB_Cart>
    implements CartService{


   @Autowired
   private ItemService itemService;


    @Autowired
    private CartMapper cartMapper;


    @Override
    public void insertCart(String workingItemId, String productId, String description, boolean in_stock, Integer quantity, Double listPrice, Double totalCost, String userId) {
        DB_Cart cart=new DB_Cart();
        cart.setDescription(description);
        cart.setQuanitity(quantity);
        cart.setInStock(in_stock?1:0);
        cart.setItemId(workingItemId);
        cart.setProductId(productId);
        cart.setTotalCost(totalCost);
        cart.setListPrice(listPrice);
        cart.setUserid(userId);

        cartMapper.insert(cart);
    }

    @Override
    public void updateQuantity(Integer quantity, String itemId, String userId) {
        double price=itemService.getItemListPrice(itemId);

        double totalCost=price*quantity;

        Wrapper<DB_Cart> wrapper=new UpdateWrapper<DB_Cart>()
                .set("quanitity",quantity)
               .set("total_cost",totalCost)
               .eq("item_id",itemId)
               .eq("userid",userId);


        cartMapper.update(wrapper);

    }


    @Override
    public void deleteItem(String itemId, String userId) {
           Wrapper<DB_Cart> wrapper=new QueryWrapper<DB_Cart>()
                   .eq("item_id",itemId)
                   .eq("userid",userId);


        cartMapper.delete(wrapper);
    }

    @Override
    public void deleteAllItem(String userId) {
          Wrapper<DB_Cart> wrapper=new QueryWrapper<DB_Cart>()
                  .eq("userid",userId);
          cartMapper.delete(wrapper);
    }

    @Override
    public void decreaseItem(String itemId, int quantity, String userId) {

    }

    @Override
    public List<DB_Cart> getCartItems(String userId) {

        Wrapper<DB_Cart> wrapper=new QueryWrapper<DB_Cart>()
               .eq("userid",userId);

        List<DB_Cart> cartItems=cartMapper.selectList(wrapper);
        return cartItems;
    }


}




